<template>
  <section>
    <el-form>
      <el-form-item>
        <el-button size="mini" type="primary" @click="addBtn">+</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="list" v-if="list">
      <el-table-column type="index"/>
      <el-table-column label="名称" prop="name"/>
      <el-table-column label="描述" prop="description"/>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="delBtn(scope.row,!scope.row.enabled)">{{scope.row.enabled?'可用':'禁用中'}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="editBtn(scope.row)" >编辑</el-button>
          <el-button size="mini" :type="scope.row.enabled?'danger':'success'" @click="delBtn(scope.row,!scope.row.enabled)">{{scope.row.enabled?'禁用':'启用'}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="editDialog" v-if="currTactics" :title="currTactics.id?'编辑':'新建'" append-to-body>
      <edit-tactics :tactics="currTactics" v-on:cancelDialog="getList"/>
    </el-dialog>
  </section>
</template>

<script>
  import editTactics from './editTactics'
  import errHandler from '@/assets/errHandler.js'
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  export default {
    name: "tacticsList",
    components:{editTactics},
    data(){
      return{
        list:null,
        currTactics:null,
        editDialog:false
      }
    },
    created(){
      this.getList()
    },
    methods:{
      ...mapActions({
        fetch: 'fetch'
      }),
      addBtn(){
        this.currTactics={name:'',description:'',data:{},id:null}
        this.editDialog=true
      },
      getList(){
        this.fetch({name:"getTacticsList",data:{}})
          .then(res=>{
            console.log('策略列表-------',res)
            this.list=res
            this.editDialog=false
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      editBtn(row){
        this.currTactics=row
        this.editDialog=true
      },
      delBtn(row,enabled){
        this.$confirm(enabled?'确认启用？':'确认禁用？')
          .then(()=> {
            this.fetch({name:"updateTactics",data:{id:row.id,enabled}})
              .then(res=>{
                this.$message({type:'success',message:enabled?'启用成功！':'禁用成功！'})
                this.getList()
              })
              .catch(err=>{
                console.log(err)
                errHandler.handle(this,err)
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消'
            })
          })
      },
      test(){
        //检测-
      }
    }
  }
</script>

<style scoped>

</style>
